package com.liuya.safe.policy.controller;

import com.liuya.common.WebUtil;
import com.liuya.safe.exception.AuthException;
import com.liuya.safe.model.SafeBusinessData;
import com.liuya.safe.policy.handler.AbstractPolicyDesignHandler;
import com.liuya.safe.policy.handler.BusinessDataPolicyDesignHandler;
import com.liuya.safe.policy.service.BusinessDataManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.StringReader;

/**
 * 查询策略测试
 */
@Controller
@Api(value = "查询策略", description = "查询策略")
@RequestMapping(value = "busiDataPolicyTest")
public class BusinessDataPolicyTestController extends AbstractPolicyTestController{
    private static final Log log = LogFactory.getLog(BusinessDataPolicyTestController.class);
    private static final int DEFAULT_QUERY_RESULT_PAGE_SIZE = 15;

    @Autowired
    @Qualifier("businessDataManagerService")
    private BusinessDataManagerService manager;

    @ApiOperation(value = "跳转到测试主页面")
//    @RequiresPermissions("sys.busiDataPolicyTest.testPolicy")
    @RequestMapping(value = "/testPolicy")
    public Object testPolicy(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.testPolicy(req, rep, modelMap);
    }

    @ApiOperation(value = "返回测试主页面")
//    @RequiresPermissions("sys.queryPolicy.returnMain")
    @RequestMapping(value = "/returnMain")
    public Object returnMain(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.returnMain(req, rep, modelMap);
    }

    @ApiOperation(value = "运行测试")
//    @RequiresPermissions("sys.queryPolicy.runTest")
    @RequestMapping(value = "/runTest")
    public Object runTest(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap) {
        return super.runTest(req, rep, modelMap);
    }

    @ApiOperation(value = "保存")
//    @RequiresPermissions("sys.queryPolicy.save")
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.save(req, rep, modelMap);
    }

    @ApiOperation(value = "设置业务数据")
//    @RequiresPermissions("sys.queryPolicy.setBusinessDataClass")
    @RequestMapping(value = "/setBusinessDataClass")
    public Object setBusinessDataClass(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.setBusinessDataClass(req, rep, modelMap);
    }

    @RequestMapping(value = "/policyRaw")
    public String policyRaw(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        String id = WebUtil.getStringParameter(req, "id", "0");
        return "redirect:/busiDataPolicyRaw/testPolicy.do?oper=loadFresh&id=" + id;
    }

    @RequestMapping(value = "/policyDesign")
    public String policyDesign(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        String id = WebUtil.getStringParameter(req, "id", "0");
        return "redirect:/busiDataPolicyDesign/editPolicy?oper=loadFresh&id=" + id;
    }

    public String getPolicyHandlerAttributeKey(HttpServletRequest req) {
        int id = WebUtil.getIntParameter(req, "id", 0);
        return "_$safeBusinessData_" + id;
    }

    public AbstractPolicyDesignHandler createPolicyHandler(HttpServletRequest req) {
        String id = WebUtil.getStringParameter(req, "id", "0");

        SafeBusinessData uc = manager.getBusinessData(id);

        com.liuya.db.sql.xml.BusinessDataType xmlUc;
        try {
            xmlUc = com.liuya.db.sql.xml.BusinessData.unmarshal(new StringReader(uc.getXmlContent()));
        } catch (Exception e) {
            throw new AuthException(e);
        }

//        return new BusinessDataPolicyDesignHandler(manager, xmlUc);
        return new BusinessDataPolicyDesignHandler(xmlUc);
    }

    protected void updateHandler(HttpServletRequest req, Object handler){
        String key = getPolicyHandlerAttributeKey(req);
        req.getSession().setAttribute(key, handler);
    }

}
